package com.example.wsh.worddemo.Notebook_Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.example.wsh.worddemo.DatabaseFile.CursorGetAll;
import com.example.wsh.worddemo.DatabaseFile.DeleteWordFromVOCAB;
import com.example.wsh.worddemo.ParseXML.PronURL;
import com.example.wsh.worddemo.R;
import cn.pedant.SweetAlert.SweetAlertDialog;


public class Notebook_Adapter extends SimpleCursorAdapter {

    private MediaPlayer EmediaPlayer = new MediaPlayer();
    private MediaPlayer AmediaPlayer = new MediaPlayer();

    private SweetAlertDialog sweetAlertDialog;

    private LinearLayout more;

    private Cursor newcursor;

    private TextView name;
    private TextView Epron;
    private TextView Apron;
    private TextView mean;
    private TextView sent;

    private ImageButton Evoice;
    private ImageButton Avoice;

    private Button test;
    private Button note_delete;

    private int currentItem = -1;

    public Notebook_Adapter(Context context, int layout, Cursor c, String[] from, int[] to,int flags)
    {
        super(context, layout, c, from, to,flags);
    }

    @Override
    public void bindView(View view, final Context context, final Cursor cursor) {

        super.bindView(view, context, cursor);

        newcursor = CursorGetAll.execute();
        more   =  view.findViewById(R.id.more);
        name   =  view.findViewById(R.id.name);
        Epron  =  view.findViewById(R.id.Epron);
        Apron  =  view.findViewById(R.id.Apron);
        mean   =  view.findViewById(R.id.mean);
        sent   =  view.findViewById(R.id.sent);

        Evoice =  view.findViewById(R.id.Evoice);
        Avoice =  view.findViewById(R.id.Avoice);

        test  = view.findViewById(R.id.test);
        note_delete = view.findViewById(R.id.note_delete);
        test.setTag(cursor.getPosition());

        // Set the text using cursor with column index.
        name.setText(cursor.getString(cursor.getColumnIndex("name")));
        Epron.setText(cursor.getString(cursor.getColumnIndex("Eps")));
        Apron.setText(cursor.getString(cursor.getColumnIndex("Aps")));
        mean.setText(cursor.getString(cursor.getColumnIndex("Means")));
        sent.setText(cursor.getString(cursor.getColumnIndex("Sents")));

       final String E = cursor.getString(cursor.getColumnIndex("Epron"));
       final String A = cursor.getString(cursor.getColumnIndex("Apron"));
       final String key = cursor.getString(cursor.getColumnIndex("name"));


        if(currentItem == (Integer) test.getTag()){
            more.setVisibility(View.VISIBLE);
        }else {
            more.setVisibility(View.GONE);
        }

        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tag = (Integer) v.getTag();
                    if (tag == currentItem) { //再次点击
                        currentItem = -1;     //给 currentItem 一个无效值
                        EmediaPlayer.reset();
                        AmediaPlayer.reset();
                    } else {
                        currentItem = tag;
                    }
                    //通知adapter数据改变需要重新加载
                    notifyDataSetChanged();
            }
        });

        note_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("WSHH","note_delete");
                sweetAlertDialog = new SweetAlertDialog(context,SweetAlertDialog.WARNING_TYPE);
                sweetAlertDialog.setTitleText("你确定?");
                sweetAlertDialog.setContentText("删除单词");
                sweetAlertDialog.setConfirmText("是");
                sweetAlertDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                DeleteWordFromVOCAB.Dosql(key);
                                changeCursor(newcursor);
                                Toast.makeText(context,"Delete Success",Toast.LENGTH_SHORT).show();
                                sweetAlertDialog.cancel();
                            }
                        });
                sweetAlertDialog.setCancelText("否");
                sweetAlertDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                Toast.makeText(context,"Cancel Delete",Toast.LENGTH_SHORT).show();
                                sweetAlertDialog.cancel();
                            }
                        })
                        .show();
                notifyDataSetChanged();

            }
        });

        Evoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    EmediaPlayer.reset();
                    EmediaPlayer.setDataSource(PronURL.ChangePronURL(E));
                    EmediaPlayer.prepareAsync();
                    EmediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) { EmediaPlayer.start();
                        }
                    });
                }catch (Exception e){
                    Log.d("WSHH",e.toString());
                }
            }
        });

        Avoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    AmediaPlayer.reset();
                    AmediaPlayer.setDataSource(PronURL.ChangePronURL(A));
                    AmediaPlayer.prepareAsync();
                    AmediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            AmediaPlayer.start();
                        }
                    });
                }catch (Exception e){
                    Log.d("WSHH",e.toString());
                }
            }
        });
    }


}
